<script>
import ValidCode from "@/components/VaildCode";

export default {
  name: "login",
  components: {
    ValidCode
  },
  data() {
    //验证码校验
    const validateCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'))
      } else if (value.toLowerCase() !== this.code) {
        callback(new Error('验证码错误'))
      } else {
        callback()
      }
    }

    return {
      code: '', //验证码组件传递的code
      user: {
        username: '',
        password: '',
        code: '', //表单里用户输入的code
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur'}
        ], password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 14, message: '长度在 6 到 14 个字符', trigger: 'blur'}
        ],
        code: [{validator: validateCode, trigger: 'blur'}]
      }
    }
  },
  methods: {
    login() {
      this.$refs['loginRef'].validate(valid => {
        if (valid) {
          //验证通过
          this.$request.post('/login', this.user).then(res => {
            console.log(res);
            if (res.code === "200") {
              this.$message.success("欢迎用户! " + res.data.username)
              this.$router.push('/')
              localStorage.setItem('honey-user', JSON.stringify(res.data)) //存储用户数据
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
    getCode(code) {
      this.$emit('update:value', '')
      this.code = code.toLowerCase()
    }
  }
}
</script>

<template>
  <div class="register">
    <div class="box">
      <div style="flex:1">
        <img src="@/assets/login.jpg" alt="" style="width: 500px; height: 500px; background-size: cover;
        background-position: center;background-repeat: no-repeat">
      </div>
      <div style="flex:1;display: flex; align-items: center;justify-content: center">
        <el-form :model="user" style="width: 80%" :rules="rules" ref="loginRef">
          <div class="title">欢迎登录后台管理系统</div>
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" size="large" id="username" placeholder="请输入用户名"
                      v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" size="large" show-password id="password" placeholder="请输入密码"
                      v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <div style="display: flex;">
              <el-input prefix-icon="el-icon-tickets" size="large" style="flex: 1" placeholder="请输入验证码"
                        v-model="user.code">
              </el-input>
              <div style="flex: 1;width: 190px;height: 36px; user-select: none">
                <valid-code @update:value="getCode"/>
              </div>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" plain size="medium" style="width: 100%" @click="login">登录</el-button>
          </el-form-item>
          <div style="display: flex;">
            <div style="flex: 1">
              <span>还没有账号？请</span>
              <span style="color: #42b0db;cursor: pointer;user-select: none" @click="$router.push('/register')">点击注册
              </span>
            </div>
            <div style="flex: 1;text-align: right;color: #0f6898;cursor: pointer">忘记密码</div>
          </div>
        </el-form>
      </div>
    </div>
  </div>

</template>

<style scoped>
.register {
  height: 100vh;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #42b0db;
}

.box {
  display: flex;
  background-color: white;
  width: 60%;
  height: 500px;
  border-radius: 5px;
  overflow: hidden;
}

.title {
  font-size: 30px;
  color: #000;
  font-weight: bold;
  text-align: center;
  padding-bottom: 20px;
}

</style>